//
//  YURecommendTagsViewController.m
//  百思不得
//
//  Created by 宋禹奇 on 15/9/5.
//  Copyright (c) 2015年 宋禹奇. All rights reserved.
//

#import "YURecommendTagsViewController.h"
#import <AFNetworking.h>
#import "YURecommendTag.h"
#import "YURecommendTagCell.h"
#import <SVProgressHUD.h>
#import <MJExtension.h>

@interface YURecommendTagsViewController ()
//数据标签
@property (nonatomic, strong) NSArray *tags;

@end

static NSString * const YUTagsId = @"tag";

@implementation YURecommendTagsViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    [self setupTableView];
    [self loadTags];
}

- (void)loadTags{
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"tag_recommend";
    params[@"action"] = @"sub";
    params[@"c"] = @"topic";
    
    [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        self.tags = [YURecommendTag objectArrayWithKeyValuesArray:responseObject];
        [self.tableView reloadData];
        
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"加载标签数据失败!"];
    }];
}

- (void)setupTableView{
    self.title = @"推荐标签";
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YURecommendTagCell class]) bundle:nil] forCellReuseIdentifier:YUTagsId];
    self.tableView.rowHeight = 70;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = YUGlobalBg;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tags.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YURecommendTagCell *cell = [tableView dequeueReusableCellWithIdentifier:YUTagsId];
    
    cell.recommendTag = self.tags[indexPath.row];
    
    return cell;
}
@end
